package juc.immutable;

/**
 * @author fangbin
 * @desc 演示栈封闭的两种情况，基本变量和对象，先验演示线程争抢带来的错误结果，然后把变量
 *  放到方法内，情况就变了
 * @date 2020/6/16 21:11
 */
public class StackConfinement implements Runnable {
    int index = 0;
    public void inThread(){
        int neverGoout = 0;
        for(int i=0;i<10000;i++){
            neverGoout++;
        }
        System.out.println(Thread.currentThread().getName()+":"+neverGoout);
    }
    @Override
    public void run() {
        for(int i=0;i<10000;i++){
            index++;
        }
        inThread();
    }

    public static void main(String[] args) throws InterruptedException {
        StackConfinement stackConfinement  = new StackConfinement();
        Thread t1 =  new Thread(stackConfinement);
        Thread t2 = new Thread(stackConfinement);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(stackConfinement.index);

    }

}
